package com.xiaoniu.workflow.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.flowable.common.rest.api.DataResponse;
import org.flowable.rest.service.api.repository.ProcessDefinitionCollectionResource;
import org.flowable.rest.service.api.repository.ProcessDefinitionResponse;
import org.flowable.spring.boot.process.ProcessEngineRestConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xiaoniu.workflow.http.response.ProcessDefinitionResp;


/**
 * @author xn071786
 * @create 2018/04/08
 **/

@Import(ProcessEngineRestConfiguration.class)
@RequestMapping(value = "api/workflow", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class ProcessDefinitionController extends ProcessDefinitionCollectionResource {
	
	private final Logger logger = Logger.getLogger(this.getClass());

    /**
     * @description:获取所有流程
     * @author fanticat
     * @date 2018/4/8
     **/
	@GetMapping("/getProcessList")
    @ResponseBody
    public ResponseEntity<List<ProcessDefinitionResp>> getProcessList(@RequestParam Map<String, String> allRequestParams, HttpServletRequest request) {
    	try {
    		 DataResponse<ProcessDefinitionResponse> dataResponse = this.getProcessDefinitions(allRequestParams, request);
    	        List<ProcessDefinitionResp> resp = new ArrayList<>();
    	        List<ProcessDefinitionResponse> processDefinitions = (List<ProcessDefinitionResponse>) dataResponse.getData();
    	        if (!CollectionUtils.isEmpty(processDefinitions)) {
    	            processDefinitions.forEach(processDefinition -> {
    	                ProcessDefinitionResp processDefinitionResp = new ProcessDefinitionResp();
    	                processDefinitionResp.setProcesskey(processDefinition.getKey());
    	                processDefinitionResp.setProcessDefinitionld(processDefinition.getId());
    	                processDefinitionResp.setProcessName(processDefinition.getName());
    	                resp.add(processDefinitionResp);
    	            });
    	            return ResponseEntity.ok(resp);
    	        } else {
    	            return ResponseEntity.ok(null);
    	        }
		} catch (Exception e) {
			logger.error("获取所有流程异常，" + e, e);
		}
    	 return ResponseEntity.ok(null);
    }
	
	
	


}
